﻿<Window 
    x:Class="LumisCalendarSync.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewModels="clr-namespace:LumisCalendarSync.ViewModels"
    Loaded="MainWindow_OnLoaded"
    >
    <Window.DataContext>
        <viewModels:MainViewModel x:Name="MainViewModel"/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel
            Grid.Row="0"
            Margin="12,12,12,12"
            Orientation="Vertical"
            VerticalAlignment="Bottom"
            HorizontalAlignment="Right"
            >
            <TextBlock
                HorizontalAlignment="Right"
                Visibility="{Binding IsLoggedOut, Converter={StaticResource BoolToVisibilityConverter}}"
                >
                <Hyperlink Click="BtnSignIn_Click" IsEnabled="{Binding IsIdle}"> Log in </Hyperlink>
            </TextBlock>
            <TextBlock
                HorizontalAlignment="Right"
                Visibility="{Binding IsLoggedIn, Converter={StaticResource BoolToVisibilityConverter}}"
                >
                <Hyperlink Click="BtnSignOut_Click" IsEnabled="{Binding IsIdle}"> Log out </Hyperlink>
            </TextBlock>
            <TextBlock Text="{Binding User.EmailAddress}"/>
        </StackPanel>
        <StackPanel
            Grid.Row="0" 
            Orientation="Horizontal"
            HorizontalAlignment="Left" 
            VerticalAlignment="Bottom"
            Margin="12,12,12,12" 
            >
            <ComboBox
                MinWidth="250" 
                ItemsSource="{Binding Calendars}" 
                SelectedValue="{Binding SelectedCalendar, Mode=TwoWay}"
                DisplayMemberPath="Name"
                IsEnabled="{Binding CanChangeCalendar}" 
                />
            <Button
                Margin="12,0"
                Width="100"
                Content="Sync"
                Command="{Binding SynchronizeCommand}"
                />
            <CheckBox
                VerticalAlignment="Center"
                Margin="12,0"
                Content="Autosync every"
                IsChecked="{Binding IsAutoSyncEnabled, Mode=TwoWay}"
                IsEnabled="{Binding CanAutosync}"
                ToolTip="When autosync is activated, the close button will minimize to system tray. Use the Tray icon to exit the app."
                />
            <TextBox 
                VerticalAlignment="Center"
                MinWidth="30"
                Text="{Binding AutoSyncInterval, Mode=TwoWay, ValidatesOnExceptions=True}"/>
            <TextBlock 
                VerticalAlignment="Center"
                Text=" minutes"/>
            <CheckBox 
                Margin="24,0,0,0"
                VerticalAlignment="Center"
                IsChecked ="{Binding RunAtStartup, Mode=TwoWay}" 
                Content="Start when I log in"/>
            <CheckBox 
                Margin="24,0,0,0"
                VerticalAlignment="Center"
                ToolTip="Do not sync Appointments older than 1 Month"
                IsChecked ="{Binding SkipOldAppointments, Mode=TwoWay}" 
                Content="Skip old appointments"/>

        </StackPanel>
        <Grid
            Grid.Row="1"
            Margin="12,0">            
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock 
                Grid.Row="0"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                Text="{Binding Events.Count, Mode=OneWay, StringFormat='You have {0} Appointments in your remote calendar. Synced means they are controlled by Lumis Calendar Sync.'}"
            >
            </TextBlock>
            <DataGrid
                Grid.Row="1"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                AutoGenerateColumns="False"
                RowHeaderWidth="0"
                IsReadOnly="True"
                ItemsSource="{Binding Events}"
                SelectedValue="{Binding SelectedEvent, Mode=TwoWay}"
                >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Synced?" Binding="{Binding IsSynchronized}"/>
                    <DataGridTextColumn Header="Subject" Width="300" Binding="{Binding Subject}"/>
                    <DataGridTextColumn Header="Location" Binding="{Binding Location}"/>
                    <DataGridTextColumn Header="Start" Binding="{Binding Start}"/>
                    <DataGridTextColumn Header="End" Binding="{Binding End}"/>
                    <DataGridTextColumn Header="Recurring?" Binding="{Binding IsRecurring}"/>
                    <DataGridTextColumn Header="Recurrence" Binding="{Binding Recurrence}"/>
                    <DataGridTextColumn Header="Reminder [min]" Binding="{Binding Reminder}"/>
                </DataGrid.Columns>           
            </DataGrid>
            <TextBlock
                Grid.Row="2"
                Grid.Column="0"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Center"
                TextWrapping="Wrap"
                >
                <Run Text="You can delete Appointments from your remote calendar, your local calendar is not affected."/>
                <LineBreak/>
                <Run Text="If you delete a synced Appointment, Lumis Calendar Sync will create and sync it again during the next sync."/>
                </TextBlock>
            <StackPanel
                Grid.Row="2"
                Grid.Column="1"
                HorizontalAlignment="Right"
                Orientation="Horizontal"
                >
                <Button
                    Width="160"
                    VerticalAlignment="Center"
                    Margin="12"
                    Command="{Binding DeleteEventCommand}"
                    Content="Delete Appointment"
                    />
                <Button
                    Width="160"
                    VerticalAlignment="Center"
                    Margin="12"
                    Content="Delete All Appointments"
                    Command="{Binding DeleteAllCommand}"
                    />
            </StackPanel>
        </Grid>
        <Expander
            Grid.Row="2"
            ExpandDirection="Up"
            Name="Expander"
            >
            <Expander.Header>
                <StackPanel
                    Orientation="Horizontal">
                    <TextBlock Text="Log file: "/>
                    <TextBlock>
                        <Hyperlink 
                            NavigateUri="{Binding LogFileName}" 
                            RequestNavigate="Hyperlink_RequestNavigate"
                            >
                            <TextBlock Text="{Binding LogFileName}"/>
                        </Hyperlink>
                    </TextBlock>
                </StackPanel>
            </Expander.Header>
            <ListBox
                x:Name="LogListBox"
                Margin="12,0"
                MinHeight="100"
                MaxHeight="300"
                ItemsSource="{Binding LogEntries}"
                />
        </Expander>
        <StatusBar
            Grid.Row="3">
            <StatusBarItem HorizontalAlignment="Left">
                <TextBlock Text="{Binding Error}" Foreground="Red"/>
            </StatusBarItem>
            <StatusBarItem HorizontalAlignment="Right">
                <TextBlock Width="120" Text="{Binding CurrentAppVersion, StringFormat='App Version: {0}'}"/>
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window>